package tool.workflow.fsSoft

import akka.stream.Materializer
import command.CommandExec
import dao._
import implicits.Implicits._
import play.api.i18n.Messages
import play.api.libs.ws.WSClient
import shared.pojo.WorkflowMission.{EachRfData, FsData}
import shared.tool.DefaultPickle
import tool.Pojo._
import tool._
import tool.plot.{Plsda3D, PlsdaBoxplot, RfPlot}
import tool.softT._
import tool.workflow.fsSoft.AllFsSoft.FSSoftBT
import utils.Utils

import java.io.File
import scala.collection.immutable.SeqMap
import upickle.default._

/**
 * Created by yz on 3/12/2020
 */
object FSRf extends FSSoftBT with RfSoftBT {

  override lazy val messageName: String = "randomForest"

  def produceSubConfigFile(workspaceDir: File, data: EachRfData) = {
    val subConfigFile = new File(workspaceDir, "calculate_config.json")
    DefaultPickle.write(data).toFile(subConfigFile)
  }

  override def resultDeal(workspaceDir: File, resultDir: File, index: Int)(implicit ws: WSClient, materializer: Materializer, messages: Messages): Unit = {
    val needConvertFileNamePrefixs = List("RF_Imp")
    val convertCommands = Tool.fileConvertCommandsIfExist(workspaceDir, needConvertFileNamePrefixs)
    CommandExec().exec { b =>
      CommandData(workspace = workspaceDir, commands = convertCommands, runFileName = "convert.sh")
    }
  }

 override def produceInput(workspaceDir: File, resultDir: File, index: Int, parentNodeStr: String,id:String)(
   implicit dao: MyDao, messages: Messages): Unit = {
    val groupFile = new File(workspaceDir.getParentFile, "group.txt")
   val value = Utils.configFile2Json(workspaceDir.getParentFile)
   val data = value.transform(reader[FsData]).rf(index)
   val groups = data.groups
   groupFile.txtLines.lineSeqMap.filter { map =>
     groups.contains(map("ClassNote"))
   }.csvLines.toTxtFile(new File(workspaceDir, "group.txt"))
    FeatureSelectionTool.produceSubInputFile(workspaceDir, parentNodeStr)
    produceSubConfigFile(workspaceDir,data)
  }

}
